package com.sky.controller.admin;

import com.sky.dto.DashboardTrendDTO;
import com.sky.result.Result;
import com.sky.service.DashboardService;
import com.sky.vo.DashboardStatisticsVO;
import com.sky.vo.DashboardTrendDataVO;
import com.sky.vo.DashboardRecentOrderVO;
import com.sky.vo.DashboardTopDocumentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 工作台控制器
 */
@RestController
@RequestMapping("/admin/dashboard")
@Api(tags = "工作台管理")
@Slf4j
public class DashboardController {
    
    @Autowired
    private DashboardService dashboardService;
    
    /**
     * 获取工作台统计数据
     * @return 统计数据
     */
    @GetMapping("/statistics")
    @ApiOperation("获取工作台统计数据")
    public Result<Map<String, Object>> getDashboardStatistics() {
        log.info("获取工作台统计数据");
        DashboardStatisticsVO statistics = dashboardService.getDashboardStatistics();
        
        // 创建一个新的Map对象，将后端的字段名映射到前端期望的字段名
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("merchantCount", statistics.getMerchantCount());
        resultMap.put("studentCount", statistics.getStudentCount());
        resultMap.put("orderCount", statistics.getOrderCount());
        resultMap.put("documentCount", statistics.getDocumentCount());
        resultMap.put("todayOrderCount", statistics.getTodayOrderCount());
        resultMap.put("todaySalesAmount", statistics.getTodaySalesAmount());
        
        // 同时添加前端期望的字段名，确保兼容性
        resultMap.put("totalMerchants", statistics.getMerchantCount());
        resultMap.put("totalStudents", statistics.getStudentCount());
        resultMap.put("todayOrders", statistics.getTodayOrderCount());
        resultMap.put("todayRevenue", statistics.getTodaySalesAmount());
        
        return Result.success(resultMap);
    }
    
    /**
     * 获取工作台趋势数据
     * @param timeRange 时间范围（today, week, month, year）
     * @return 趋势数据列表
     */
    @GetMapping("/trend")
    @ApiOperation("获取工作台趋势数据")
    public Result<List<DashboardTrendDataVO>> getDashboardTrendData(
            @RequestParam(value = "timeRange", defaultValue = "week") String timeRange) {
        log.info("获取工作台趋势数据，前端时间范围：{}", timeRange);
        
        // 转换前端时间范围格式到后端期望的格式
        String backendTimeRange = convertToBackendTimeRange(timeRange);
        log.info("转换为后端时间范围：{}", backendTimeRange);
        
        DashboardTrendDTO trendDTO = new DashboardTrendDTO();
        trendDTO.setTimeRange(backendTimeRange);
        List<DashboardTrendDataVO> trendData = dashboardService.getDashboardTrendData(trendDTO);
        return Result.success(trendData);
    }
    
    /**
     * 转换前端时间范围格式到后端期望的格式
     */
    private String convertToBackendTimeRange(String frontendTimeRange) {
        switch (frontendTimeRange) {
            case "today":
                return "today";
            case "7days":
                return "week";
            case "30days":
                return "month";
            case "180days":
                // 前端6个月对应后端的year逻辑
                return "year";
            case "365days":
                // 前端1年也对应后端的year逻辑
                return "year";
            default:
                return "week";
        }
    }
    
    /**
     * 获取最新订单列表
     * @param limit 获取数量限制
     * @return 最新订单列表
     */
    @GetMapping("/recent-orders")
    @ApiOperation("获取最新订单列表")
    public Result<List<DashboardRecentOrderVO>> getRecentOrders(
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        log.info("获取最新订单列表，限制数量：{}", limit);
        List<DashboardRecentOrderVO> recentOrders = dashboardService.getRecentOrders(limit);
        return Result.success(recentOrders);
    }
    
    /**
     * 获取热门文档列表
     * @param limit 获取数量限制
     * @return 热门文档列表
     */
    @GetMapping("/top-documents")
    @ApiOperation("获取热门文档列表")
    public Result<List<DashboardTopDocumentVO>> getTopDocuments(
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        log.info("获取热门文档列表，限制数量：{}", limit);
        List<DashboardTopDocumentVO> topDocuments = dashboardService.getTopDocuments(limit);
        return Result.success(topDocuments);
    }
}